package com.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

/**
 * apache httpclient连接池库实现http get post请求
 * 
 * @author zincredible
 * @date 2019/04/15 15:43:36
 */
public class ApacheHttpUtil {

	private final static int CONNECT_TIMEOUT = 10000;// 设置从连接池获取连接实例的超时10s
	private final static int SOCKET_TIMEOUT = 10000;// 设置读取超时10s
	private final static HttpClientPoolManager mgr = HttpClientPoolManager.getInstance();

	public static String get(String url) {
		CloseableHttpClient httpClient = mgr.getHttpClient();
		HttpGet httpget = new HttpGet(url);
		String content = null;
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpget);
			content = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

			if (response != null)
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return content;
	}

	public static JsonObject post(String url, Map<String, String> params) {
		HttpPost httpPost = new HttpPost(url);
		setRequestConfig(httpPost);
		setPostParams(httpPost, params);
		CloseableHttpResponse response = null;
		InputStream in = null;
		JsonObject object = null;
		try {
			response = mgr.getHttpClient().execute(httpPost, HttpClientContext.create());
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				in = entity.getContent();
				String result = IOUtils.toString(in, "utf-8");
				Gson gson = new Gson();
				object = gson.fromJson(result, JsonObject.class);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null)
					in.close();
				if (response != null)
					response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return object;
	}

	/**
	 * 设置请求参数
	 * 
	 * @param httpPost
	 * @param params
	 */
	private static void setPostParams(HttpPost httpPost, Map<String, String> params) {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Set<String> keys = params.keySet();
		for (String key : keys) {
			nvps.add(new BasicNameValuePair(key, params.get(key)));
		}
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 对http请求进行基本设置
	 * 
	 * @param httpRequestBase http请求
	 */
	private static void setRequestConfig(HttpRequestBase httpRequestBase) {
		RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECT_TIMEOUT) // 设置从连接池获取连接实例的超时
				.setConnectTimeout(CONNECT_TIMEOUT)// 设置连接超时
				.setSocketTimeout(SOCKET_TIMEOUT) // 设置读取超时
				.build();

		httpRequestBase.setConfig(requestConfig);
	}
}
